package view;

import java.awt.event.ItemEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import model.ClassModel;
import model.Staff;
import model.Subject;
import util.DataUtil;

public class ViewAddSubjectSchedule extends javax.swing.JDialog {

    private ClassModel clm;

    public ViewAddSubjectSchedule(java.awt.Frame parent, boolean modal, ClassModel clm) {
        super(parent, modal);
        initComponents();
        this.clm = clm;
        setLocationRelativeTo(null);
        setCombobox();
    }

    public void setCombobox() {
        DefaultComboBoxModel dcb = new DefaultComboBoxModel();
        dcb.addElement("Select Subject");
        try {
            String sql = "select Class.ClassName, Subject.SubjectID,Subject.SubName,Subject.NoOfSession from Class inner join Course on Class.CourseID = Course.CourseID\n"
                    + "inner join Subject_Course on Course.CourseID = Subject_Course.CourseID\n"
                    + "inner join Subject on Subject_Course.SubjectID = Subject.SubjectID where class.ClassID = ?";
            PreparedStatement pst = DataUtil.getConnection().prepareStatement(sql);
            pst.setString(1, clm.getClassId());
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                Subject sbj = new Subject();
                sbj.setSubjectId(rs.getString("SubjectID"));
                sbj.setSubjectName(rs.getString("SubName"));
                sbj.setNoSession(rs.getInt("NoOfSession"));
                dcb.addElement(sbj);
            }
            cbbSubject.setModel(dcb);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbbHourEnd = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cbbSesEnd = new javax.swing.JComboBox();
        cbbSubject = new javax.swing.JComboBox();
        cbbSesStart = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbbHourStart = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        cbbSession = new javax.swing.JComboBox();
        cbbMinuteStaff = new javax.swing.JComboBox();
        cbbMinuteEnd = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbbStaff = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        cbbHourEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jLabel1.setText("Subject Name :");

        cbbSesEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "am", "pm" }));

        cbbSubject.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "java" }));
        cbbSubject.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbSubjectItemStateChanged(evt);
            }
        });

        cbbSesStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "am", "pm" }));

        jLabel2.setText("Session :");

        cbbHourStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jLabel10.setText("Start Time :");

        jLabel7.setText("End Time :");

        jButton1.setText("Add subject for schedule");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cbbMinuteStaff.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }));

        cbbMinuteEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }));

        jLabel5.setText(":");

        jLabel6.setText(":");

        jLabel3.setText("Staff Name :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1)
                    .addComponent(cbbSession, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbbSubject, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbbHourEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbbMinuteEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(cbbHourStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbbMinuteStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbbSesStart, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbbSesEnd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(cbbStaff, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(82, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbbSession, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(cbbHourStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbSesStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbMinuteStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cbbHourEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbSesEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbMinuteEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(41, 41, 41)
                .addComponent(jButton1)
                .addGap(35, 35, 35))
        );

        jLabel4.setText("add Subject schedule");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(145, 145, 145)
                        .addComponent(jLabel4)))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(77, 77, 77))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbbSubjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbSubjectItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            if (cbbSubject.getSelectedItem().toString().equals("Select Subject")) {
                cbbStaff.removeAllItems();
                cbbSession.removeAllItems();
            } else {
                Subject sbj = (Subject) cbbSubject.getSelectedItem();
                DefaultComboBoxModel dcb = new DefaultComboBoxModel();
                try {
                    String sql = "select Staff.StaffID,Staff.Username,Staff.FullName from Subject inner join Subject_Staff on Subject.SubjectID = Subject_Staff.SubjectID\n"
                            + "inner join Staff on Subject_Staff.StaffID = Staff.StaffID where Subject.SubjectID = ?";
                    PreparedStatement pst = DataUtil.getConnection().prepareStatement(sql);
                    pst.setString(1, sbj.getSubjectId());
                    ResultSet rs = pst.executeQuery();
                    while (rs.next()) {
                        Staff st = new Staff();
                        st.setStaffId(rs.getString("StaffID"));
                        st.setUserName(rs.getString("Username"));
                        st.setFullName(rs.getString("FullName"));
                        dcb.addElement(st);
                    }
                    cbbStaff.setModel(dcb);

                    try {
                        DefaultComboBoxModel modelSession = new DefaultComboBoxModel();
                        int noofsession = sbj.getNoSession();
                        for (int i = 1; i <= noofsession; i++) {
                            modelSession.addElement(i);
                        }
                        cbbSession.setModel(modelSession);
                    } catch (Exception e) {
                        e.printStackTrace();
                        JOptionPane.showMessageDialog(this, "loi");
                    }

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_cbbSubjectItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(cbbSubject.getSelectedIndex()==0){
            JOptionPane.showMessageDialog(this, "Choose subject ...");
        }else{
            Subject sbj = (Subject) cbbSubject.getSelectedItem();
            Staff st = (Staff) cbbStaff.getSelectedItem();
            int session = Integer.parseInt(cbbSession.getSelectedItem().toString());
            String hourStart = cbbHourStart.getSelectedItem().toString();
            String minuteStart = cbbMinuteStaff.getSelectedItem().toString();
            String SesStart = cbbSesStart.getSelectedItem().toString();
            String startTime = hourStart + " : " + minuteStart + " : " + SesStart;
            String hourEnd = cbbHourEnd.getSelectedItem().toString();
            String minuteEnd = cbbMinuteEnd.getSelectedItem().toString();
            String SesSEnd = cbbSesEnd.getSelectedItem().toString();
            String endTime = hourEnd + " : " + minuteEnd + " : " + SesSEnd;
            ViewAddClassSchedule.model.addRow(new Object[]{sbj.getSubjectId(),sbj,session,startTime,endTime,st});
            this.dispose();
        }
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbbHourEnd;
    private javax.swing.JComboBox cbbHourStart;
    private javax.swing.JComboBox cbbMinuteEnd;
    private javax.swing.JComboBox cbbMinuteStaff;
    private javax.swing.JComboBox cbbSesEnd;
    private javax.swing.JComboBox cbbSesStart;
    private javax.swing.JComboBox cbbSession;
    private javax.swing.JComboBox cbbStaff;
    private javax.swing.JComboBox cbbSubject;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
